home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / sbin / ispell-autobuildhash < prev    next >
Text File  |  2009-05-15  |  10KB  |  295 lines

  1. #!/usr/bin/perl -w
  2. #
  3. #  script for ispell hash autorebuild in Debian systems
  4. #
  5. # Copyright 2003-2009 Agustin Martin Domingo <agmartin@debian.org>
  6. #
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21. #
  22.  
  23. sub usage {
  24.     print STDERR "\nUsage:\tispell-autobuildhash [--debug] [--force]\n"
  25.     . "\n"
  26.     . "Options:\n"
  27.     . "\t--debug         Show ispell-autobuildhash debugging information\n"
  28.     . "\t--force         Do the job regardless of versions comparisons\n";
  29. }
  30.  
  31. sub debugprint {
  32.     print STDERR "@_\n" if $debug;
  33. }
  34.  
  35. sub mymessage{
  36.     my $message  = join(" ",@_);
  37.     my $question = "dictionaries-common/ispell-autobuildhash-message";
  38.     my $hashfile = '';
  39.  
  40.     if ( $dict ) {
  41.     $hashfile = "$dict.hash";
  42.     } else {
  43.     $hashfile = "dictionaries-common";
  44.     }
  45.  
  46.     subst($question,"xxpell","ispell");
  47.     subst($question,"XXpell","Ispell");
  48.     subst($question,"hashfile",$hashfile);
  49.     subst($question,"errormsg",$message);
  50.     fset ($question,"seen","false");
  51.     title("dictionaries-common: Running ispell-autobuildhash");
  52.     input("critical",$question);
  53.     go ();
  54. }
  55.  
  56. sub old_myerror {
  57.     print "
  58. ** Error: @_
  59.  
  60. This error was caused by package providing hash $dict, although it
  61. can be made evident during other package postinst. Please complain
  62. to the maintainer of package providing hash $dict. In the meantime
  63. you might need to remove package providing $dict.hash\n";
  64.     myclean();
  65.     exit 1;
  66. }
  67.  
  68. sub myerror {
  69.     mymessage @_;
  70.     $build_status = "error";
  71. #    myclean();
  72. #    exit 1;
  73. }
  74.  
  75. sub myclean {
  76.     unlink <$tempfile*>;
  77.     rmdir $tmpdir;
  78. }
  79.  
  80.  
  81. # ---------------------------------------------------------------------
  82. #      What to do if a signal is trapped
  83. # ---------------------------------------------------------------------
  84.  
  85. sub trap {
  86.     myclean();
  87.     exit 1
  88. }
  89.  
  90. # ---------------------------------------------------------------------
  91. #      Handle autorebuilding
  92. # ---------------------------------------------------------------------
  93.  
  94. sub autorebuild {
  95.     my $dict = shift ||                          # The dictionary name
  96.     myerror "No argument passed to function autorebuild";
  97.     my $zmwl = "/usr/share/ispell/$dict.mwl.gz"; # the compressed munched wordlist
  98.     my $aff  = "/usr/lib/ispell/$dict.aff";      # the aff file
  99.     my $hash = "/var/lib/ispell/$dict.hash";     # the hash file
  100.     my $desc = "/usr/share/ispell/$dict.desc";   # the .desc file
  101.  
  102.     print STDERR "ispell-autobuildhash: Processing \'$dict\' dict\n";
  103.  
  104.     if ( -e $aff ){
  105.     if ( -e $zmwl ){
  106.         system ("gzip -dc $zmwl > $tempfile") == 0
  107.         or myerror "Could not gunzip the munched wordlist for $dict";
  108.         system ("buildhash -s $tempfile $aff $hash") == 0
  109.         or myerror "Could not build the hash file for $dict" ;
  110.  
  111.     } elsif ( -e $desc ) {
  112.         if ( -x "/usr/sbin/update-ispell-hash" ) {
  113.         system ("update-ispell-hash -d $dict > /dev/null") == 0
  114.             or myerror "Problems running update-ispell-hash for $dict";
  115.         debugprint "Found and run update-ispell-hash";
  116.         } elsif ( -x "/usr/sbin/update-ipolish-hash" ) {
  117.         system ("update-ipolish-hash -d $dict > /dev/null") == 0
  118.             or myerror "Problems running update-ipolish-hash for $dict";
  119.         debugprint "Found and run update-ipolish-hash";
  120.         } else {
  121.         myerror "Could not find any of update-ispell-hash, update-ipolish-hash"
  122.         }
  123.     } else {
  124.         myerror "Could not find $zmwl";
  125.     }
  126.     } else {
  127.     myerror "Could not find affix file $aff";
  128.     }
  129. }
  130.  
  131. # ---------------------------------------------------------------------
  132. #                   Get ispell compat version
  133. # ---------------------------------------------------------------------
  134.  
  135. sub get_ispell_compat {
  136.     my @internal;
  137.     my $ispell_compat;
  138.     if ( -e $ispellcompatfile ){
  139.     open (COMPAT,"$ispellcompatfile");
  140.     chomp ( $ispell_compat = <COMPAT> );
  141.     close COMPAT;
  142.     } elsif ( -e "/usr/bin/ispell" ){
  143. #    $ispell_compat = `ispell -v | head -n 1 | sed 's/.*Version \([^\ ]*\).*/\1/g'`
  144.     $ispell_compat = `ispell -v | head -n 1`;
  145.     @internal = split(" ",$ispell_compat);
  146.     $ispell_compat = $internal[4];
  147.     } else {
  148.     myerror "ispell is not installed";
  149.     }
  150.     return $ispell_compat;
  151. }
  152.  
  153. # ---------------------------------------------------------------------
  154. #                        The main program
  155. # ---------------------------------------------------------------------
  156.  
  157. use Debian::DictionariesCommon q(dico_checkroot);
  158. use Debconf::Client::ConfModule q(:all);
  159. use Getopt::Long;
  160.  
  161. dico_checkroot();
  162.  
  163. $SIG{INT}  = \&trap;
  164. $SIG{KILL} = \&trap;
  165. $SIG{TERM} = \&trap;
  166.  
  167. $tmpdir           = "/tmp/ispell-auto-$$";
  168. $tempfile         = "$tmpdir/ispellhash";
  169. $compatdir        = "/var/lib/ispell";
  170. $ispellcompatfile = "/usr/share/ispell/ispell.compat";
  171.  
  172. $force        = '';
  173. $debug        = '';
  174. $build_status = '';
  175.  
  176. GetOptions ('debug' => \$debug,
  177.         'force' => \$force) or usage();
  178.  
  179. mkdir($tmpdir,0700)        || die "can't mkdir $tmpdir: $!";
  180.  
  181. if ( -x "/usr/bin/ispell" ){
  182.     $ispell_compat = get_ispell_compat();
  183.     foreach $dict ( <$compatdir/*.compat> ){
  184.     $build_status   = '';
  185.     my $dict_compat = '';
  186.     $dict =~ s/\.compat$//;
  187.     $dict =~ s/.*\///;
  188.     if ( $force ) {
  189.         $dict_compat = "forced";
  190.         autorebuild $dict;
  191.     } else {
  192.         open (COMPAT,"$compatdir/$dict.compat");
  193.         $dict_compat = <COMPAT>;
  194.         close COMPAT;
  195.         $dict_compat = 0 if not $dict_compat;
  196.         chomp $dict_compat;
  197.         autorebuild $dict if ($ispell_compat ne $dict_compat);
  198.     }
  199.     debugprint "$dict => ispell_compat: [$ispell_compat]; dict_compat: [$dict_compat]";
  200.     if ( not $build_status ){
  201.         debugprint " +++ Updating $compatdir/$dict.compat";
  202.         open (COMPAT,">","$compatdir/$dict.compat");
  203.         print COMPAT "$ispell_compat\n";
  204.         close COMPAT;
  205.     } else {
  206.         debugprint " --- $compatdir/$dict.compat not updated because of an error";
  207.     }
  208.  
  209.     unlink <$tempfile*>;
  210.     }
  211. } else {
  212.     debugprint " ispell is not installed. Doing nothing";
  213. }
  214.  
  215. myclean ();
  216.  
  217. __END__
  218.  
  219. =head1 NAME
  220.  
  221. B<ispell-autobuildhash> - Autobuilding the ispell hash file for some dicts
  222.  
  223. =head1 SYNOPSIS
  224.  
  225.  ispell-autobuildhash [--force]
  226.  
  227.    Options:
  228.     --debug      Show some extra ispell-autobuildhash information.
  229.     --force      Rebuild the hash file for all dicts providing a
  230.                  compat file skipping the test.
  231.  
  232. =head1 DESCRIPTION
  233.  
  234. B<ispell-autobuildhash> is a script that will manage ispell hash files
  235. autobuild, intended to be called from the dictionaries-common tools.
  236. Depending on the ispell
  237. compatibility level and on the compatibility level used for the hash file
  238. if present, will decide whether it must be rebuilt or not. This script will
  239. only work on ispell packages prepared to use it, it will do nothing for other
  240. ispell dict packages.
  241.  
  242. =head1 OPTIONS
  243.  
  244. --debug      Show some extra ispell-autobuildhash information.
  245. --force      Rebuild the hash file for all dicts providing a compat
  246.              file regardless of the compatibility levels found.
  247.  
  248. =head1 PACKAGE MAINTAINERS
  249.  
  250. To use this system, just provide a F<$dict.compat> file in F</var/lib/ispell>
  251. (I<$dict> stands or the hash or affix file basename). Put a "0" in it or just
  252. create an empty one with touch. The way things are done depends on whether
  253. your dict has a single source or a multiple source like ipolish has.
  254.  
  255. The most common case is having a single source. In this case you must put
  256. the F<$dict.aff> file at F</usr/lib/ispell/$dict.aff> as usual and the munched
  257. wordlist compressed with gzip at F</usr/share/ispell/$dict.mwl.gz>.
  258.  
  259. This script supports the way ipolish allows selection of subdictionaries
  260. to be put in the hash file. It will detect the presence of  the main
  261. language .desc file and call update-ipolish-hash as appropriate.
  262.  
  263. Although this script looks for the existence of a update-ispell-hash script
  264. with supposedly similar functionality to update-ipolish-hash, this latter
  265. does not really exists, although it might be added at some time to ispell or
  266. to the dictionaries-common system, if enough demand is found.
  267.  
  268. If your package provides more than one ispell dictionary you will need to
  269. do the steps above for each dictionary.
  270.  
  271. Dictionaries-common scripts will call internally this script and create a
  272. hash file at F</var/lib/ispell/$dict.hash>. You must set a symlink to that
  273. file from F</usr/lib/ispell/$dict.hash>. You are also suggested to create
  274. an empty file at F</var/lib/ispell/$dict.hash> in the install target of
  275. your package build process. This empty file will be overwritten when the
  276. real hash is created, but will make the hash be removed at package
  277. removal without any magic being done in the postrm and will also help to
  278. keep track about which package owns that file.
  279.  
  280. If you use B<debhelper> you may want to pass B<-Xvar/lib/ispell> option
  281. to B<dh_md5sums> call in your F<debian/rules> file, to avoid B<debsums>
  282. complaining about those hash and compat files.
  283.  
  284. B<ispell> maintainer should also call this script from package postinst.
  285. When comparing versions it will get the ispell version from file
  286. F</usr/share/ispell/ispell.compat>, or if it does not exists, from the
  287. upstream version as given in the first line of 'B<ispell -vv> output.
  288.  
  289. =head1 AUTHORS
  290.  
  291. Agustin Martin <agmartin@debian.org>
  292.  
  293. =cut
  294.  
  295.